<?php
namespace app\api\controller;

use CodeDredd\Soap\SoapClient;
use think\facade\Db;

class Server
{

    public function index()
    {
        $xml_data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Header/><soapenv:Body><p228:AddCardResponseV02 xmlns:p228=\"http://fleetCredit02.comdata.com/maintenance/\"><response><accountCode>W-990</accountCode><customerId>CLSWX</customerId><discretionaryData xsi:nil=\"true\"/><trackingNumber xsi:nil=\"true\"/><cardNumber>5563054294555532</cardNumber><employeeNumber>HZ1922222221212</employeeNumber><responseCode>00000</responseCode><responseDescription>CARD ADDED</responseDescription><cardToken>366G36Q7K7</cardToken></response></p228:AddCardResponseV02></soapenv:Body></soapenv:Envelope>";

        $xml_read = new \DOMDocument();
        $xml_read->loadXML($xml_data);
        foreach ($xml_read->lastChild->lastChild->lastChild->lastChild->childNodes as $dom){
            if($dom->tagName == "employeeNumber")
                var_dump($dom->textContent);
            if($dom->tagName == "cardToken")
                var_dump($dom->textContent);
        }
    }

    public function index1()
    {
        $soapClient = new \SoapClient("https://api.iconnectdata.com/FleetCreditWS/services/FleetCreditWS0200/wsdl/FleetCreditWS0200.wsdl", array(
            'trace' => TRUE,
            'exceptions' => TRUE,
            'cache_wsdl' => WSDL_CACHE_BOTH,
            'stream_context' => stream_context_create(array(
                'http' => array(
                    'header' => 'SOAPAction: "http://fleetCredit02.comdata.com/FleetCreditWS0200/addCardV02"'
                ),
            )),
        ));
        $xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:main="http://fleetCredit02.comdata.com/maintenance/">
   <soapenv:Header>
          <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
              <wsse:Username>WSPPTCO</wsse:Username>
              <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">ge$$RAdDZb3p</wsse:Password>
            </wsse:UsernameToken>
          </wsse:Security>
     </soapenv:Header>
   <soapenv:Body>
      <main:CardProfileLimitUpdateRequest>
                                    <requestUtil>
                                        <accountCode>W-990</accountCode>
                                        <customerId>CLSWX</customerId>
                                        <productCode>10000</productCode>
                                    </requestUtil>
                                    <empNumCardNumFlag>C</empNumCardNumFlag>
                                    <empNumCardNumValue>5563054295391077</empNumCardNumValue>
                                    <profileLimits>
                                        <openCloseFlag>O</openCloseFlag>
                                        <productAvailableFlag>Y</productAvailableFlag>
                                        <transactionDollarLimit>50</transactionDollarLimit>
                                        <dailyDollarLimit>50</dailyDollarLimit>
                                        <dailyTransactionLimit>999</dailyTransactionLimit>
                                        <cycleDollarLimit>50</cycleDollarLimit>
                                        <cycleTransactionLimit>999</cycleTransactionLimit>
                                        <cycleType>T</cycleType>
                                        <cycleDay>T</cycleDay>
                                    </profileLimits>
                                </main:CardProfileLimitUpdateRequest>
   </soapenv:Body>
</soapenv:Envelope>';   // data from the form, e.g. some ID number

        $response = $soapClient->__doRequest($xml_post_string, 'https://api.iconnectdata.com/FleetCreditWS/services/FleetCreditWS0200', 'addCardV02', SOAP_1_2);
        $xml_read = new \DOMDocument();
        $xml_read->loadXML($response);
        /** 先判断错误 **/
        $error_code = '';
        $error_string = '';
        foreach ($xml_read->lastChild->lastChild->lastChild->childNodes as $dom){
            if($dom->tagName == "faultcode")
                $error_code     = $dom->textContent ?? '';
            if($dom->tagName == "faultstring")
                $error_string   = $dom->textContent ?? '';
        }
        if(empty($error_code)){
            /** **/
            $list     = [];
            foreach ($xml_read->lastChild->lastChild->lastChild->childNodes as $dom){
                if($dom->tagName == "responseDescription"){
                    $list['responseDescription'] = $dom->textContent ?? '';
                }
            }

//            foreach ($xml_read->lastChild->lastChild->lastChild->childNodes as $dom){
//                if($dom->tagName == "cardDetails")
//                {
//                    foreach ($dom->childNodes as $dom_1){
//                        if($dom_1->tagName == "cardExpiration")
//                        {
//                            $list['cardExpiration'] = $dom_1->textContent ?? '';
//
//                        }
//                    }
//                }
//            }
            print_r($list);
        }else{
            echo $error_string;
        }

    }


}
